package com.tanhua.admin.listener;

import com.tanhua.autoconfig.template.GreenTemplate;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.model.mongo.Movement;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
@Slf4j
@Component
public class MovementListener {

    @DubboReference
    private MovementApi movementApi;

    @Autowired
    private GreenTemplate aliyunGreenTemplate;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    value = "tanhua.audit.queue",
                    durable = "true"
            ),
            exchange = @Exchange(
                    value = "tanhua.audit.exchange",
                    type = ExchangeTypes.TOPIC),
            key = {"audit.movement"})
    )
    public void listenCreate(String movementId) {
        System.out.println("内容审核：id="+movementId);
            //1、根据动态id查询动态
            Movement movement = movementApi.findById(movementId);
            //对于RocketMQ消息有可能出现重复，解决方法判断 (幂等性)
            Integer state = 0;
        try {
            if(movement != null && movement.getState() == 0) {
                //1.只有文本没有图片
                if(movement.getTextContent() != null && movement.getMedias() == null) {
                    //审核文本
                    Map<String, String> textScan = aliyunGreenTemplate.greenTextScan(movement.getTextContent());
                    if(textScan != null) {
                        String textSuggestion = textScan.get("flag");
                        if ("no".equals(textSuggestion)){
                            state = 2;//审核不通过
                        }else if("yes".equals(textSuggestion)) {
                            state = 1;//审核通过
                        }
                    }
                }
                //2.只有图片没有文本
                if(movement.getTextContent() == null && movement.getMedias() != null) {
                    //审核图片
                    Map<String, String> imageScan = aliyunGreenTemplate.imageScan(movement.getMedias());
                    if(imageScan != null) {
                        String imageSuggestion = imageScan.get("flag");
                        if ("no".equals(imageSuggestion)){
                            state = 2;
                        }else if("yes".equals(imageSuggestion)) {
                            state = 1;
                        }
                    }
                }
                //3.既有文本又有图片
                //审核文本、审核图片
                Map<String, String> textScan = aliyunGreenTemplate.greenTextScan(movement.getTextContent());
                Map<String, String> imageScan = aliyunGreenTemplate.imageScan(movement.getMedias());
                if(textScan != null && imageScan != null) {
                    String textSuggestion = textScan.get("flag");
                    String imageSuggestion = imageScan.get("flag");
                    if ("no".equals(textSuggestion) || "no".equals(imageSuggestion)){
                        state = 2;
                    }else if("yes".equals(textSuggestion) && "yes".equals(imageSuggestion)) {
                        state = 1;
                    }
                }
            }
        } catch (Exception e) {
            log.error("审核失败！动态id="+movementId+"，错误信息："+e.getMessage());
        }
        movementApi.update(movementId,state);
    }
}